﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;

namespace Painter.Component
{
    public class ScaleGrid:Grid
    {
        public static DependencyProperty ShowGridProperty =
            DependencyProperty.Register("ShowGrid", typeof(bool), typeof(ScaleGrid), new PropertyMetadata(true));
        public static DependencyProperty ScaleBoxProperty =
            DependencyProperty.Register("ScaleBox", typeof(double), typeof(ScaleGrid), new PropertyMetadata(10.0));

        public bool ShowGrid
        {
            get{return (bool)GetValue(ShowGridProperty);}
            set
            {
                SetValue(ShowGridProperty, value);
                Background = (value ? mGridBrush : null);
            }
        }
        public double ScaleBox
        {
            get{return (double)GetValue(ScaleBoxProperty);}
            set{
                SetValue(ScaleBoxProperty, value);
                mGridBrush = new DrawingBrush(
                    new GeometryDrawing(new SolidColorBrush(Colors.Transparent),
                    new System.Windows.Media.Pen(new SolidColorBrush(Colors.Black), 1.0) { DashStyle = DashStyles.Dot },
                    new RectangleGeometry(new Rect(0, 0, ScaleBox, ScaleBox))))
                {
                    Stretch = Stretch.None,
                    TileMode = TileMode.Tile,
                    Viewport = new Rect(0.0, 0.0, ScaleBox, ScaleBox),
                    ViewportUnits = BrushMappingMode.Absolute
                };
                if ((bool)GetValue(ShowGridProperty))
                    Background = mGridBrush;
            }
        }
        DrawingBrush mGridBrush = new DrawingBrush(
            new GeometryDrawing(new SolidColorBrush(Colors.Transparent),
            new System.Windows.Media.Pen(new SolidColorBrush(Colors.Black), 1.0) { DashStyle = DashStyles.Dot },
            new RectangleGeometry(new Rect(0, 0, 20, 20))))
        {
            Stretch = Stretch.None,
            TileMode = TileMode.Tile,
            Viewport = new Rect(0.0, 0.0, 20, 20),
            ViewportUnits = BrushMappingMode.Absolute
        };
        public ScaleGrid()
        {
            Background = mGridBrush;
            IsHitTestVisible = false;
            VerticalAlignment = VerticalAlignment.Top;
            HorizontalAlignment = HorizontalAlignment.Left;
        }
    }
}
